package zdream.pmfield.stage.control;

import java.util.function.Consumer;

import zdream.pmfield.stage.IController;
import zdream.pmfield.stage.action.AttackAction;
import zdream.pmfield.stage.message.ActionRequestMessage;
import zdream.pmfield.stage.message.ActionRequestReply;
import zdream.pmfield.stage.object.FoeActor;

/**
 * 这个 AI 是, 如果有移动力, 则会尝试往目标位置走.
 *
 * @author Zdream
 * @date 2023-12-16
 * @since 0.0.1
 */
public class MovementController implements IController {
	private int targetX;
	private Consumer<FoeActor> onCompleted;

	public MovementController(int targetX) {
		this.targetX = targetX;
	}

	@Override
	public ActionRequestReply createReply(ActionRequestMessage request) {
		final ActionRequestReply reply = request.createReplyTemplate();
		final FoeActor foe = request.foe;

		AttackAction action = new AttackAction(foe);
		reply.action = action;

		final int currentX = action.foe.seat.x;
		if (currentX == targetX) {
			if (onCompleted != null) {
				onCompleted.accept(foe);
			}
			return null;
		}

		action.move = foe.findMoveToMove();
		action.targetSeat = targetX > currentX ? action.foe.seat.right : action.foe.seat.left;
		return reply;
	}
}
